<?php
	if(!defined('__ARRAYLIST_PHP__'))
	{
		define('__ARRAYLIST_PHP__', true);
		
		/**
		 * @class A wrapper class for an array
		 */
		class ArrayList
		{
			/**
			 * @var The wrapped array
			 */
			protected $m_Array;
			
			/**
			 * @method Constructor, calls method clear()
			 */
			function __construct()
			{
				$this->clear();
			}
			
			/**
			 * @method Gets the size of this array
			 * 
			 * @return The size of this array
			 */
			function size()
			{
				return count($this->m_Array);
			}
			
			/**
			 * @method Adds an object to this array
			 * 
			 * @param $object The object to add to this array
			 */
			function add($object)
			{
				$this->m_Array[] = $object;
			}
			
			/**
			 * @method Gets the object at the specified index.
			 * 
			 * @return The object at the specified index, or NULL if no such index exists
			 */
			function get($index)
			{
				$object = NULL;
				
				if(($index > -1) && ($index < $this->size()))
				{
					$object = $this->m_Array[$index];
				}
				
				return $object;
			}
			
			/**
			 * @method Gets the index of a certain object.
			 * 
			 * @return The index of a certain object, or false if that object can not be found
			 */
			function indexOf($object)
			{
				return array_search($object, $this->m_Array, true);
			}
			
			/**
			 * @method Removes an object at a specified index. If the specified index is out of bounds
			 * (i.e. smaller than 0 or larger than the capacity) nothing happens
			 * 
			 * @param $index The specified index to remove
			 */
			function removeAt($index)
			{
				if(($index > -1) && ($index < $this->size()))
				{
					$this->m_Array[$index] = NULL;
					$this->m_Array = array_values($this->m_Array);
				}
			}
			
			/**
			 * @method Removes a certain object from the array, if that object is found.
			 * If the object is not found, nothing happens
			 * 
			 * @param $object The object to remove
			 */
			function remove($object)
			{
				$remove = true;
				
				if(indexOf($object) === false)
				{
					$remove = false;
				}
				
				if($remove)
				{
					removeAt($this->indexOf($object));
				}
			}
			
			/**
			 * @method Clears the array, making it empty
			 */
			function clear()
			{
				$this->m_Array = array();
			}
			
			/**
			 * @method Sets the wrapped array
			 * 
			 * @param $array The array to wrap
			 */
			function setArray($array)
			{
				$this->m_Array = $array;
			}
			
			/**
			 * @method Gets the wrapped array
			 * 
			 * @return The wrapped array
			 */
			function getArray()
			{
				return $this->m_Array;
			}
			
			/**
			 * @method Sorts the array. Override this method in a derived class
			 * to introduce usort, and a custom sort rule
			 */
			function sortArrayList()
			{
				sort($this->m_Array);
			}
			
			/**
			 * @method Reverses the order of the elements in the wrapped array
			 */
			function reverseArrayList()
			{
				$this->m_Array = array_reverse($this->m_Array);
			}
		}
	}
?>
